//
// This is the Compiler's Companion a program to integrate make's and Edit
// more cleanly. Version 2.0
//
// Copyright(C) 1992 Daryll Strauss
//
//    This program is shareware; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 1, or (at your option)
//    any later version as long as you leave all the references to the fact
//    that it is shareware and covered by the GNU General Public License in
//    tact.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//	Please see the README.rtf file for more details on the shareware
//	distribution and the GNU General Public License.
//

#import "Defaults.h"
#import <objc/objc.h>
#import <appkit/nextstd.h>
#import <defaults/defaults.h>

//
// Basic support functions
//

static BOOL parse_bool(char *bool_str)
{
	if (!strcasecmp(bool_str, "yes")) return TRUE;
	if (!strcasecmp(bool_str, "no")) return FALSE;
	if (!strcasecmp(bool_str, "true")) return TRUE;
	if (!strcasecmp(bool_str, "false")) return FALSE;
	return(FALSE);
}

static void Read_Default_Value(Full_Defaults_Vector def, char *value)
{
	switch (def.type) {
	case default_string:
		if (*(char**)def.current_value) free(*(char**)def.current_value);
		*(char**)def.current_value=malloc(strlen(value)+1);
		strcpy(*(char**)def.current_value, value);
		break;
	case default_const_string:
		*(char**)def.current_value=value;
		break;
	case default_int:
		*(int*)def.current_value=atoi(value);
		break;
	case default_float:
		*(float*)def.current_value=atof(value);
		break;
	case default_bool:
		*(BOOL*)def.current_value=parse_bool(value);
		break;
	default:
		fprintf(stderr, "Unknown type of default\n");
	}
}

static void Write_Default_Value(char *app_name, Full_Defaults_Vector def)
{
	char value[64];
	
	switch (def.type) {
	case default_string:
	case default_const_string:
		NXWriteDefault(app_name, def.name, *(char**)def.current_value);
		break;
	case default_int:
		sprintf(value, "%d", *(int*)def.current_value);
		NXWriteDefault(app_name, def.name, value);
		break;
	case default_float:
		sprintf(value, "%f", *(int*)def.current_value);
		NXWriteDefault(app_name, def.name, value);
		break;
	case default_bool:
		if (*(BOOL*)def.current_value)
			NXWriteDefault(app_name, def.name, "TRUE");
		else
			NXWriteDefault(app_name, def.name, "FALSE");
		break;
	default:
		fprintf(stderr, "Unknown type of default\n");
	}
}

void Register_Full_Defaults(char *app_name, Full_Defaults_Vector *defaults)
{
	struct _NXDefault *v;
	int i, cnt=0;

	while (defaults[cnt].type!=default_end) cnt++;
	cnt++;
	v = malloc(cnt*sizeof(struct _NXDefault));
	for (i=0; i<cnt; i++) {
		v[i].name=defaults[i].name;
		v[i].value=defaults[i].default_value;
	}
	NXRegisterDefaults(app_name, v);
	free(v);
}

void Get_Full_Defaults(char *app_name, Full_Defaults_Vector *defaults)
{
	int i=0;
	char *value;
	
	while (defaults[i].type!=default_end) {
		value=(char*)NXGetDefaultValue(app_name, defaults[i].name);
		Read_Default_Value(defaults[i], value);
		i++;
	}
}

void Reset_Defaults(Full_Defaults_Vector *defaults)
{
	int i=0;

	while (defaults[i].type!=default_end) {
		Read_Default_Value(defaults[i], defaults[i].default_value);
		i++;
	}
}

void Put_Full_Defaults(char *app_name, Full_Defaults_Vector *defaults)
{
	int i=0;
	
	while (defaults[i].type!=default_end) {
		Write_Default_Value(app_name, defaults[i]);
		i++;
	}
}

void Get_Default_By_Name(char *app_name, char *default_name, 
	Full_Defaults_Vector *defaults)
{
	int i=0;
	char *value;
	
	while (defaults[i].type!=default_end) {
		if (!strcmp(defaults[i].name, default_name)) {
			value=(char*)NXGetDefaultValue(app_name, default_name);
			Read_Default_Value(defaults[i], value);
			return;
		}
		i++;
	}
	fprintf(stderr, "Can't find the requested default\n");
}

void Put_Default_By_Name(char *app_name, char *default_name,
	Full_Defaults_Vector *defaults)
{
	int i=0;
	
	while (defaults[i].type!=default_end) {
		if (!strcmp(defaults[i].name, default_name)) {
			Write_Default_Value(app_name, defaults[i]);
			return;
		}
		i++;
	}
	fprintf(stderr, "Can't find the requested default\n");
}
